package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.ReportDao;
import com.itheima.pojo.Setmeal;
import com.itheima.service.ReportService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportDao reportDao;

    @Override
    public List<Integer> getMemberReport(List<String> list1) {
        List<Integer> list = new ArrayList();
        for (String s : list1) {
            String endDate = s+"-31";
            Integer i = reportDao.getMemberReport(endDate);
            if(i == null){
                list.add(0);
            }else {
                list.add(i);
            }
        }

        return list;
    }

    @Override
    public List<Setmeal> getSetmeal() {
        return reportDao.getSetmeal();
    }

    @Override
    public List<Map<String, String>> getBySetmealIdOrderCount(List<Setmeal> setmeals) {
        List<Map<String,String>> list = new ArrayList<>();
        for (Setmeal setmeal : setmeals) {
            Map<String,String> map = new HashMap<>();
            Integer i = reportDao.getBySetmealIdOrderCount(setmeal.getId());
            System.out.println(setmeal.getId());
            map.put("name",setmeal.getName());
            if(i != null){
                map.put("value",i.toString());
            }else{
                map.put("value",0+"");
            }
            System.out.println(map);
            list.add(map);
        }
        return list;
    }

    // 运营数据统计
    @Override
    public Map<String, Object> getBusinessReportData() {

        Map<String,Object> map = new HashMap<>();


        // 获取当前日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String reportDate = sdf.format(new Date());
        map.put("reportDate",reportDate);

        //获取本周一的日期
        String firstDay4ThisMonth = sdf.format(DateUtils.getThisWeekMonday());

        // 获取 本月 的月初和月末
        int index = reportDate.lastIndexOf("-");
        String substring = reportDate.substring(0, index);
        String startDate = substring+"-1";
        String endDate = substring+"-31";

        // 统计当天新增 会员数
        Integer todayNewMember = reportDao.getByNowDate(reportDate);
        map.put("todayNewMember",todayNewMember);

        // 获取 总会员数
        Integer totalMember = reportDao.getSetmealCount();
        map.put("totalMember",totalMember);

        // 获取本周新增 会员 数
        Integer thisWeekNewMember = reportDao.getWeekNewMember(firstDay4ThisMonth);
        map.put("thisWeekNewMember",thisWeekNewMember);

        // 获取本月新增会员数
        Integer thisMonthNewMember = reportDao.gettMonthNewMember(startDate, endDate);
        map.put("thisMonthNewMember",thisMonthNewMember);

        //获取 今日预约数
        Integer todayOrderNumber = reportDao.getTodayOrderNumber(reportDate);
        map.put("todayOrderNumber",todayOrderNumber);

        //  获取今日到诊数
        Integer todayVisitsNumber = reportDao.getTodayVisitsNumber(reportDate);
        map.put("todayVisitsNumber",todayVisitsNumber);

        //获取本月 预约数
        Integer thisMonthOrderNumber = reportDao.getMonthOrderNumber(startDate);
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);

        //统计本月就诊数
        Integer thisMonthVisitsNumber = reportDao.getMonthVisitsNumber(startDate);
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);

        //统计本周的预约数
        Integer thisWeekOrderNumber = reportDao.getMonthOrderNumber(firstDay4ThisMonth);
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);

        //统计本周的就诊数
        Integer thisWeekVisitsNumber = reportDao.getMonthVisitsNumber(firstDay4ThisMonth);
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);

        //获取 预约量最多的套餐名称以及占比 hotSetmeal :[]
        //查询套餐名称以及套餐预约的数量 ， 以及占比
        List<Map> hotSetmeal = reportDao.findHotSetmeal();
        map.put("hotSetmeal",hotSetmeal);
        return map;
    }

}
